/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.medikuak;

import igraf.ItzuliInterfazea;
import igraf.Panela;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.MaskFormatter;

import dBasea.Medikua;

public class MedikuaEsleitu extends Panela{
	
private static final long serialVersionUID = -3686061110565795802L;
private JButton ok;
private JButton ezeztatu;
private JLabel klinika;
private JLabel data1;
private JTextField abi;
private JLabel data2;
private JTextField tele;
private JTextField nif;
private JLabel bezNif;
private JLabel nola;
private JLabel nola2;
private JComboBox hiri;
private JButton bilatu;
private Medikua medi=new Medikua();

public MedikuaEsleitu(String tituloa,final ItzuliInterfazea itzuliInterfazea) {
	super(tituloa);
JPanel erdikoPanela= this.getErdikoPanela();
	
	erdikoPanela.setBackground(SystemColor.activeCaption);
	erdikoPanela.setLayout(new BorderLayout(0, 0));

	JPanel erdikoa = new JPanel();
	erdikoa.setBackground(SystemColor.activeCaption);
	
	JPanel xxx=new JPanel();
	xxx.setBackground(SystemColor.activeCaption);
	erdikoa.add(xxx);
	
	JPanel y=new JPanel();
	y.setBackground(SystemColor.activeCaption);
	erdikoa.add(y);
	
	JPanel xx=new JPanel();
	xx.setBackground(SystemColor.activeCaption);
	bezNif=new JLabel("Mediku NAN:");
	bezNif.setFont(new Font("Arial", Font.PLAIN, 14));
		nif=new JTextField(20);
		nif.addKeyListener(new KeyAdapter() {//limitea NAN
			
			public void keyTyped(KeyEvent arg0) {
				if (nif.getText().length()>=10){
					arg0.consume();
					}
			}
		});
	xx.add(bezNif);
	xx.add(nif);
	erdikoa.add(xx);
	
	erdikoa.add(xx);
	
	bilatu = new JButton("Bilatu");
	bilatu.setIcon(new ImageIcon(MedikuaEsleitu.class.getResource("/Argazkia/lupaa.png")));
	xx.add(bilatu);
	
	JPanel arrib = new JPanel();
	arrib.setBackground(SystemColor.activeCaption);
	FlowLayout fl_arrib = new FlowLayout();
	fl_arrib.setHgap(15);
	fl_arrib.setVgap(9);
	arrib.setLayout(fl_arrib);
	klinika=new JLabel("Klinika:");
	klinika.setFont(new Font("Arial", Font.PLAIN, 14));
	hiri=new JComboBox();
	try {
		boxCarga();
	}
	catch (SQLException e) {
		JOptionPane.showMessageDialog(null, "Ezin izan da kargatu DB-tik klinika zerrenda", "Oker", JOptionPane.ERROR_MESSAGE);				
		e.printStackTrace();
	}
	
	arrib.add(klinika);arrib.add(hiri);
	erdikoa.add(arrib);
	

	
	JPanel erdi = new JPanel();
	erdi.setBackground(SystemColor.activeCaption);
	FlowLayout fl_erdi = new FlowLayout();
	fl_erdi.setHgap(15);
	fl_erdi.setVgap(8);
	erdi.setLayout(fl_erdi);
	data1=new JLabel("Hasiera data :");
	data1.setFont(new Font("Arial", Font.PLAIN, 14));
	try {
		abi=new JFormattedTextField(new MaskFormatter("  ##/##/####  "));
		abi.setColumns(10);
	} catch (ParseException e2) {

		e2.printStackTrace();
	}
	erdi.add(data1);
	erdi.add(abi);
	erdikoa.add(erdi);
	
	nola = new JLabel("ee/hh/uuuu ");
	erdi.add(nola);
	
	JPanel bost = new JPanel();
	bost.setBackground(SystemColor.activeCaption);
	FlowLayout fl_bost = new FlowLayout();
	fl_bost.setHgap(15);
	fl_bost.setVgap(8);
	bost.setLayout(fl_bost);
	data2=new JLabel("Bukaera data:");
	data2.setFont(new Font("Arial", Font.PLAIN, 14));
	data2.setVerticalAlignment(SwingConstants.BOTTOM);
	try {
		tele=new JFormattedTextField(new MaskFormatter("   ##/##/####  "));
		tele.setColumns(10);
	} catch (ParseException e1) {
		
		e1.printStackTrace();
	}
	bost.add(data2);
	bost.add(tele);
	erdikoa.add(bost);
	
	JPanel sei = new JPanel();
	sei.setBackground(SystemColor.activeCaption);
	bost.setBackground(SystemColor.activeCaption);
	
	nola2 = new JLabel("ee/hh/uuuu ");
	bost.add(nola2);
	FlowLayout fl_sei = new FlowLayout();
	fl_sei.setHgap(15);
	fl_sei.setVgap(8);
	sei.setLayout(fl_sei);
		
	erdikoa.add(sei);
	
	erdikoPanela.add(erdikoa, BorderLayout.CENTER);
	erdikoa.setLayout(new GridLayout(8,1));
	JPanel gs=new JPanel();
	gs.setBackground(SystemColor.activeCaption);
	erdikoa.add(gs);
	
	
	
	FlowLayout fl_behekoa = new FlowLayout();
	fl_behekoa.setVgap(20);
	fl_behekoa.setHgap(110);
	JPanel behekoa = new JPanel(fl_behekoa);
	behekoa.setBackground(SystemColor.activeCaption);
	ok = new JButton("Medikua esleitu");
	ok.setHorizontalAlignment(SwingConstants.LEFT);
	ok.setFont(new Font("Arial", Font.PLAIN, 14));
	behekoa.add(ok);
	ezeztatu = new JButton("Ezeztatu");
	
	ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
	ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
	behekoa.add(ezeztatu);
	erdikoPanela.add(behekoa, BorderLayout.SOUTH);
	
	ezeztatu.addActionListener(new ActionListener() {			
		@Override
		public void actionPerformed(ActionEvent arg0) {
			nif.setEnabled(true);
			abi.setEnabled(false);
			tele.setEnabled(false);
			;itzuliInterfazea.itzuliLehenPanela();
		}
	});
	
	bilatu.addActionListener(new ActionListener() {			
		@Override
		public void actionPerformed(ActionEvent arg0) {
		
			if (nif.getText().isEmpty()) {
				JOptionPane.showMessageDialog(null, "Sartu NAN zenbakia, mesedez!", "Oker", JOptionPane.ERROR_MESSAGE);
			}
			else {
				try {
					if (medi.existitzenDaMedikua(nif.getText())) {
						
						JOptionPane.showMessageDialog(null, "Badago medikua!", null, JOptionPane.INFORMATION_MESSAGE);
						ezarri(true);
					}
					else {
						JOptionPane.showMessageDialog(null, "Ez da existitzen sartu duzuna", "Oker", JOptionPane.ERROR_MESSAGE);
					}
				}
				catch (SQLException e) {					
					e.printStackTrace();
				}
			}
		}
	});
	ok.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			try {
				if (!zerbaitIdatzi()) {
					JOptionPane.showMessageDialog(null,
							"Sartu behar duzu zerbait zuriunetan\n"
									+ "Ezin da eremu hutsik egon", "Oker",
							JOptionPane.ERROR_MESSAGE);
				
				} else {
					try {
						Object hir= hiri.getSelectedItem();
						String hu=hir.toString();
						DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
						Date da= formatter.parse(abi.getText());
						long data = da.getTime();
						Date dak= formatter.parse(tele.getText());
						long data2= dak.getTime();
				
						medi.medikuaEsleitu(nif.getText(), hu , data,data2 );
						JOptionPane.showMessageDialog(null,
								""+nif.getText()+" medikua ondo esleitu da "+hu+" klinikarekin",
								"", JOptionPane.INFORMATION_MESSAGE);
						eremuakGarbitu();
						ezarri(false);
						nif.setEnabled(true);
					}
					catch(NumberFormatException nfe) {
						JOptionPane.showMessageDialog(null,
								"Zenbakiak sartu data eremuan",
								"Oker", JOptionPane.ERROR_MESSAGE);
					}
				}
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null,
						"Data txarto jarrita edo zuriuneak daude!",
						"Oker", JOptionPane.ERROR_MESSAGE);
				e.printStackTrace();
			}
		}
	});
}


private void boxCarga() throws SQLException {
	List<String> lista = medi.getKlinikak();
	hiri.removeAllItems();
	if (lista != null) {
		for (String hiria: lista) {
			hiri.addItem(hiria);
		}
	}
}
private void ezarri(boolean balioa) {
	
	abi.setEnabled(balioa);
	tele.setEnabled(balioa);
	if (balioa) {
		
		nif.setEnabled(false);
	}

}
@Override
public void eremuakGarbitu() {
	nif.setText("");
	abi.setText("");
	tele.setText("");


}
private boolean zerbaitIdatzi() {
	boolean aurki = false;
	 aurki=nif.getText().length() > 0 && abi.getText().length() > 0 && tele.getText().length() > 0 ;
	return aurki;
}
}